<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DB_Project
 *
 * @author Bolle
 */
class DB_Project {
    
 
	private $db;
	
	
	
	function __construct(){
		
		$this->db = new Database();
               
	}
	
	
	// Metoden tar inn et objekt av MProject som argument og registrerer det i databasen

		public function registerMproject($project, $uid){
	
			$id 			= mysql_real_escape_string($project->getMproject_id());
        	$title 			= mysql_real_escape_string($project->getTitle());
        	$start			= mysql_real_escape_string($project->getStart());
        	$end			= mysql_real_escape_string($project->getEnd());
        	$est_time		= mysql_real_escape_string($project->getEst_time());
        	$pri_id			= mysql_real_escape_string($project->getPri_id());
        	$private		= mysql_real_escape_string($project->getPrivate());
        	$customer_id    = mysql_real_escape_string($project->getCustomer_id());
        	$company_id		= mysql_real_escape_string($project->getCompany_id());
            $info			= mysql_real_escape_string($project->getInfo());
            
            $uid			= mysql_real_escape_string($uid);
                
			$sql1 = "INSERT INTO ax_mproject VALUES (0, '".$title."', '".$start."', '".$end."','".$est_time."','".$pri_id."','".$private."','".										$customer_id."','".$company_id."','".$info."');";
			
			if($this->db->query($sql1)){
			
				$id = mysql_insert_id();
			
				$sql2 = "INSERT INTO ax_mproject_user VALUES (".$id.",".$uid.");";
	
				if($this->db->query($sql2)){
		
					//echo "Win!";
					return true;
		
				}
				else
				{ 
					//Secho "Failed!";
                	return false;
				}
				}
			else{
			return false; 
			echo "FaIL!";
			}
        }
	
	
        public function registerPproject($u){
        
        	$id 		= mysql_real_escape_string($u->getMproject_id());
        	$title 		= mysql_real_escape_string($u->getTitle());
        	$start		= mysql_real_escape_string($u->getStart());
        	$end		= mysql_real_escape_string($u->getEnd());
        	$est_time	= mysql_real_escape_string($u->getEst_time());
        	$pri_id		= mysql_real_escape_string($u->getPri_id());
        	$private	= mysql_real_escape_string($u->getPrivate());
        	$customer_id= mysql_real_escape_string($u->getCustomer_id());
        	$company_id	= mysql_real_escape_string($u->getCompany_id()); 
        	$info		= mysql_real_escape_string($u->getInfo()); 
        
        
        	$sql = "INSERT INTO ax_pproject VALUES ('".$id."', '".$title."', '".$start."', '".$end."','".$est_time."','".$pri_id."','".$private."','".							    $customer_id."','".$customer_id.", '".$info."');";
	
			if($this->db->query($sql)){
		
				return true;
			
			}
		
			else return false;
    
		}
		
		

		public function getProjects($id)
		{
			
			$id 		= mysql_real_escape_string($id);
		    $list 	= false;
            $sql 	= sprintf("SELECT * from ax_mproject m, ax_mproject_user mp, ax_user where m.mproject_id = mp.mproject_id and mp.user_id = ".$id." and 								ax_user.user_id =  ".$id.";");
            if($this->db->query($sql)){
            while ($this->db->fetch_row())
            {	echo "Succes 1st!";
            	$mpid  = $this->db->record['mproject_id'];
                $project = new Project($mpid);
                $project->setInfo($this->db->record['info']);
                $project->setTitle($this->db->record['title']);
                $project->setStart($this->db->record['start']);
                $project->setEnd($this->db->record['end']);
                $project->setEst_time($this->db->record['est_time']);
                $project->setPri_id($this->db->record['pri_id']);
                $project->setPrivate($this->db->record['private']);
                $project->setCustomer_id($this->db->record['customer_id']);
                $project->setCompany_id($this->db->record['company_id']);
               
               
               	$pplist = false;
                // SQL som henter ut delprosjekt (Pproject) fra databasen til den tilhørende bruker og det sist hentede MProject-objektet
                $sql2 = sprintf("SELECT ax_pproject.* from ax_pproject, ax_mproject, ax_pproject_user,ax_mproject_pproject, ax_user WHERE 											ax_pproject.pproject_id = ax_pproject_user.pproject_id AND  ax_pproject_user.user_id = ".$id." AND 															ax_user.user_id = ".$id." and ax_pproject.pproject_id = ax_mproject_pproject.pproject_id AND ax_mproject_pproject.mproject_id = ".							$mpid." AND ax_mproject.mproject_id = ".$mpid.";");
                
                echo "IF 1 STARTS";
            	if($this->db->query($sql2)){
                while ($this->db->fetch_row())
                {
                	echo "Succes 2nd!";
                	$pid = $this->db->record['pproject_id'];
                	$pproject = new PProject($pid);
                	$pproject->setInfo($this->db->record['info']);
                	$pproject->setTitle($this->db->record['title']);
                	$pproject->setStart($this->db->record['start']);
                	$pproject->setEnd($this->db->record['end']);
                	$pproject->setEst_time($this->db->record['est_time']);
                	$pproject->setPri_id($this->db->record['pri_id']);
                	$pproject->setPrivate($this->db->record['private']);
                	$pproject->setCustomer_id($this->db->record['customer_id']);
                	$pproject->setCompany_id($this->db->record['company_id']);
                	
                	 
                	$sql2 = "SELECT * FROM ax_tasks WHERE pproject_id = ".$pid." and mpoject_id = ".$pid.";";
                	
                	$tlist = false;
                	
                	if($this->db->query($sql2)){
                	
                	while ($this->db->fetch_row())
                	{
                		echo "Success 3!";
                		$task = new Task($this->db->record['task_id']);
                		$task->setTitle($this->db->record['title']);
                		$task->setDescription($this->db->record['description']);
                		$task->setTime($this->db->record['time']);
                		$task->setStatus_id($this->db->record['status_id']);
                		$task->setPproject_id($this->db->record['pproject_id']);
                		$task->setMproject_id($this->db->record['mproject_id']);
                		
                		$tlist[] = $task;
                
                	}
                	}else{
                		echo "Fail3";
                	}
                	$pproject->setTasks($tasks);
                	$pplist[] = $pproject;
                
                }
                }
                else{
                echo "Feil2";
                }
                $project->setPProjects($pplist);
                
                $sql2 = "SELECT * FROM ax_tasks WHERE mproject_id = ".$mpid." and ppoject_id = 0;";
                $mtlist = false;

                
                while ($this->db->fetch_row())
                	{
                
                		$task = new Task($this->db->record['task_id']);
                		$task->setTitle($this->db->record['title']);
                		$task->setDescription($this->db->record['description']);
                		$task->setTime($this->db->record['time']);
                		$task->setStatus_id($this->db->record['status_id']);
                		$task->setPproject_id($this->db->record['pproject_id']);
                		$task->setMproject_id($this->db->record['mproject_id']);
                		
                		$mtlist[] = $task;
                
                	}
                
                $project->setTasks($mtlist);
                
                $list[] = $project;
            }
            }
            else{
            echo "Feil 1";
            }
            
            
            return $list;
	

		}
		
		public function updateMProject($project){
		
		
			$id 		= mysql_real_escape_string($project->getMproject_id());
        	$title 		= mysql_real_escape_string($project->getTitle());
        	$start		= mysql_real_escape_string($project->getStart());
        	$end		= mysql_real_escape_string($project->getEnd());
        	$est_time	= mysql_real_escape_string($project->getEst_time());
        	$pri_id		= mysql_real_escape_string($project->getPri_id());
        	$private	= mysql_real_escape_string($project->getPrivate());
        	$customer_id= mysql_real_escape_string($project->getCustomer_id());
        	$company_id	= mysql_real_escape_string($project->getCompany_id()); 
        	$info		= mysql_real_escape_string($project->getInfo()); 
        	
        	$sql = "UPDATE ax_mproject SET title = '".$title."', end = '".$end."', info = '".$info."',start = '".$start."', est_time = ".$est_time.", private = ".						$private." WHERE mproject_id = ".$id.";";
        		   
        	if($this->db->query($sql)){
        		echo "Success!!!";
        		return true;
        	
        	}
        	echo "Fail!";
        	return false;
		
		}

}
?>
